package dhm.com.dhmshop.framework.module.home.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.facebook.drawee.view.SimpleDraweeView;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import dhm.com.dhmshop.R;
import dhm.com.dhmshop.adapter.home.ListEliteGoodsAdapter;
import dhm.com.dhmshop.base.BaseActiity;
import dhm.com.dhmshop.base.Presenter.PressenterImpl;
import dhm.com.dhmshop.base.netWork.Constant;
import dhm.com.dhmshop.base.netWork.LoginContract;
import dhm.com.dhmshop.framework.base.BaseActivity;
import dhm.com.dhmshop.framework.base.BaseModel;
import dhm.com.dhmshop.framework.base.MyBaseViewHolder;
import dhm.com.dhmshop.framework.module.commodity.activity.CommodityDetailsActivity;
import dhm.com.dhmshop.framework.module.home.entity.ListEliteGoodsEntity;
import dhm.com.dhmshop.framework.module.home.model.ListEliteGoodsModel;
import dhm.com.dhmshop.framework.module.home.view.ListEliteGoodsView;
import dhm.com.dhmshop.framework.utils.GlideUtil;
import dhm.com.dhmshop.framework.utils.StringUtil;

/**
 * 有好货商品列表
 */
public class ListEliteGoodsActivity extends BaseActivity<ListEliteGoodsModel> implements LoginContract.IView {

    private ListEliteGoodsAdapter listEliteGoodsAdapter;
    private int offset;
    private PressenterImpl pressenter;
    private XRecyclerView listEliteGoodsRecy;
    private LinearLayout image;
    private ListEliteGoodsEntity listEliteGoodsEntity;
    private List<ListEliteGoodsEntity.DataBean> list;

    @SuppressLint("NewApi")
    @Override
    protected void initView() {
        getWindow().setStatusBarColor(Color.WHITE);
        ImageView eiteGoodsFan = findViewById(R.id.eitegoods_fan);
        image = findViewById(R.id.image);
        listEliteGoodsRecy = findViewById(R.id.list_elite_goods);
        pressenter=new PressenterImpl();
        pressenter.attachView(this);
        LinearLayoutManager manager=new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        offset=1;


        listEliteGoodsRecy.setLoadingMoreEnabled(true);
        listEliteGoodsRecy.setPullRefreshEnabled(true);
        listEliteGoodsRecy.setLayoutManager(manager);

        listEliteGoodsRecy.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                offset=1;
                search();
            }

            @Override
            public void onLoadMore() {
                search();
            }
        });


        //添加Android自带的分割线
        listEliteGoodsRecy.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));

        eiteGoodsFan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pressenter.detachView();
                finish();
            }
        });
        search();
    }

    private void search() {
        Map<String,String> map=new HashMap<>();
        map.put("token",Constant.TOKEN);
        map.put("page",offset+"");
        pressenter.sendMessage(ListEliteGoodsActivity.this,Constant.ListEliteGoods,map,ListEliteGoodsEntity.class);

    }


    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

    }

    @Override
    protected int intiLayoutId() {
        return R.layout.activity_list_elite_goods;
    }

    @Override
    protected ListEliteGoodsModel initModel() {
        return null;
    }

    @Override
    public void requesta(Object data) {
        if (data instanceof ListEliteGoodsEntity){
            ListEliteGoodsEntity listEliteGoodsEntity= (ListEliteGoodsEntity) data;
            if (listEliteGoodsEntity.getCode()==1){
                image.setVisibility(View.GONE);
                listEliteGoodsRecy.setVisibility(View.VISIBLE);
                if (offset==1){
                    list = listEliteGoodsEntity.getData();
                    listEliteGoodsAdapter = new ListEliteGoodsAdapter(ListEliteGoodsActivity.this);
                    listEliteGoodsAdapter.setaddClick(new ListEliteGoodsAdapter.OnShopClick() {
                        @Override
                        public void item(int posttion) {
                            Intent intent=new Intent(ListEliteGoodsActivity.this, CommodityDetailsActivity.class);
                            intent.putExtra("gid",list.get(posttion).getGoods_id());
                            intent.putExtra("shop_id",list.get(posttion).getShop_id());
                            startActivity(intent);

                        }
                    });
                    listEliteGoodsRecy.setAdapter(listEliteGoodsAdapter);

                }else {
                    list.addAll(listEliteGoodsEntity.getData());
                }
                offset++;
                listEliteGoodsAdapter.setShopList(list);
            }else {
                if (offset==1){
                    image.setVisibility(View.VISIBLE);
                    listEliteGoodsRecy.setVisibility(View.GONE);
                }else {
                    Toast.makeText(this, "暂无更多数据", Toast.LENGTH_SHORT).show();
                }
            }
            listEliteGoodsRecy.refreshComplete();
            listEliteGoodsRecy.loadMoreComplete();
        }

    }

    @Override
    public void fail(String error) {

    }

}
